package service

import (
	"html/template"
	"log"
	"net/http"
	"os"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"github.com/unrolled/render"
)

// NewServer configures and returns a Server.
func NewServer() *negroni.Negroni {

	formatter := render.New(render.Options{
		Directory:  "templates",       //指定模板目录
		Extensions: []string{".html"}, //指定模板文件的扩展名
		IndentJSON: true,
	})

	n := negroni.Classic()
	mx := mux.NewRouter()

	initRoute(mx, formatter)

	n.UseHandler(mx)
	return n
}

//initRoute
func initRoute(mx *mux.Router, formatter *render.Render) {
	webRoot := os.Getenv("WEBROOT")
	if len(webRoot) == 0 {
		if root, err := os.Getwd(); err != nil {
			panic("Could not retrive working directory")
		} else {
			webRoot = root
			//fmt.Println(root)
		}
	}

	mx.HandleFunc("/assets/js/hello.js", handlejs(formatter, webRoot)).Methods("GET")
	mx.HandleFunc("/assets/css/style.css", handlecss(formatter, webRoot)).Methods("GET")
	// mx.HandleFunc("/", register(formatter, webRoot)).Methods("GET")
	mx.HandleFunc("/register", register(formatter, webRoot)).Methods("GET")
	mx.HandleFunc("/jsinfo.html", jsinfo(formatter, webRoot)).Methods("GET")
	// mx.HandleFunc("/register", register(formatter, webRoot)).Methods("POST")
	mx.HandleFunc("/registerMessage", registerMessage(formatter)).Methods("POST")
	//加载静态文件系统
	mx.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir(webRoot+"/"))))
	// mx.PathPrefix("/filesystem").Handler(http.FileServer(http.Dir(webRoot)))

}

func registerMessage(formatter *render.Render) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		// fmt.Println(req.URL.Path)
		err := req.ParseForm()
		// fmt.Println(err)
		// fmt.Println(req.Form.Get("username"))
		// fmt.Println(req.Form.Get("password"))
		if err != nil {
			panic("Could not parse the form")
		}
		formatter.HTML(w, http.StatusOK, "registerMessage", struct {
			UserName string `json:"username"`
			PassWord string `json:"password"`
		}{UserName: req.Form.Get("username"), PassWord: req.Form.Get("password")})
	}
}

func jsinfo(formatter *render.Render, root string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		//fmt.Println("method:", r.Method) //获取请求的方法
		if r.Method == "GET" {
			//这里添加js传值的info文件，注意路径需要自己手动改变
			var jsinfoPage string = root + "/assets/jsinfo.html"
			//fmt.Println(registerPage)
			t, _ := template.ParseFiles(jsinfoPage)
			log.Println(t.Execute(w, nil))
		}
	}
}

func register(formatter *render.Render, root string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		//fmt.Println("method:", r.Method) //获取请求的方法
		if r.Method == "GET" {
			//这里添加注册页面的html文件，注意路径需要自己手动改变
			var registerPage string = root + "/assets/register.html"
			//fmt.Println(registerPage)
			t, _ := template.ParseFiles(registerPage)
			log.Println(t.Execute(w, nil))
		}
	}
}

func handlecss(formatter *render.Render, root string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		//fmt.Println("method:", r.Method) //获取请求的方法
		if r.Method == "GET" {
			//这里添加注册页面的css文件，注意路径需要自己手动改变
			var cssPage string = root + "/assets/css/style.css"
			//fmt.Println(cssPage)
			t, _ := template.ParseFiles(cssPage)
			log.Println(t.Execute(w, nil))
		}
	}
}

func handlejs(formatter *render.Render, root string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		//fmt.Println("method:", r.Method) //获取请求的方法
		if r.Method == "GET" {
			//这里添加注册页面的js文件，注意路径需要自己手动改变
			var jsPage string = root + "/assets/js/hello.js"
			//fmt.Println(jsPage)
			t, _ := template.ParseFiles(jsPage)
			log.Println(t.Execute(w, nil))
		}
	}
}
